package nl.boelen.game.gfx;

import nl.boelen.game.client.Client;

public class Screen {
    public static final int MAP_WIDTH = 64;
    public static final int MAP_WIDTH_MASK = MAP_WIDTH - 1;

    public static final byte BIT_MIRROR_X = 0x01;
    public static final byte BIT_MIRROR_Y = 0x02;

    public int[] pixels;

    public int xOffset = 0;
    public int yOffset = 0;

    public int width;
    public int height;

    public SpriteSheet spriteSheet;

    private Client client;

    public Screen(Client client, int width, int height, SpriteSheet spriteSheet) {
        this.width = width;
        this.height = height;
        this.spriteSheet = spriteSheet;
        this.client = client;

        pixels = new int[width * height];
    }

    public void render(int xPos, int yPos, int tile, int color, int mirrorDir, int scale) {
        render(xPos, yPos, tile, color, mirrorDir, scale, false);
    }

    public void render(int xPos, int yPos, int tile, int color, int mirrorDir, int scale, boolean light) {
        xPos -= xOffset;
        yPos -= yOffset;
        int xTile = tile % 32;
        int yTile = tile / 32;
        int scaleMap = scale - 1;

        boolean mirrorX = (mirrorDir & BIT_MIRROR_X) > 0;
        boolean mirrorY = (mirrorDir & BIT_MIRROR_Y) > 0;

        int tileOffset = (xTile * 8) + (yTile * 8) * spriteSheet.width;
        for (int y = 0; y < 8; y++) {
            int yPixel = y + yPos + (y * scaleMap) - ((scaleMap * 8) / 2);

            int ySheet = y;
            if (mirrorY) {
                ySheet = 7 - y;
            }

            for (int x = 0; x < 8; x++) {
                int xPixel = x + xPos + (x * scaleMap) - ((scaleMap * 8) / 2);
                int xSheet = x;
                if (mirrorX) {
                    xSheet = 7 - x;
                }
                int col = (color >> (spriteSheet.pixels[xSheet + ySheet * spriteSheet.width + tileOffset] * 8)) & 255;
                if (col < 255) {
                    for (int yScale = 0; yScale < scale; yScale++) {
                        if (yPixel + yScale < 0 || yPixel + yScale >= height) {
                            continue;
                        }
                        for (int xScale = 0; xScale < scale; xScale++) {
                            if (xPixel + xScale < 0 || xPixel + xScale >= width) {
                                continue;
                            }
                            // if (light) {
                            // int range = 20;
                            //
                            // int playerX = client.player.x - xOffset;
                            // int playerY = client.player.y - yOffset;
                            //
                            // if ((playerX + playerY + 4) - (xPixel + yPixel) >
                            // range
                            // || (playerX - playerY + 4)
                            // - (xPixel - yPixel) > range
                            // || (playerX + playerY + 4)
                            // - (xPixel + yPixel) < -range
                            // || (playerX - playerY + 4)
                            // - (xPixel - yPixel) < -range) {
                            // col = 0;
                            // }
                            // }
                            pixels[(xPixel + xScale) + (yPixel + yScale) * width] = col;
                        }
                    }
                }
            }
        }
    }

    public void setOffset(int xOffset, int yOffset) {
        this.xOffset = xOffset;
        this.yOffset = yOffset;
    }
}
